package com.marvels.approve.task;

import javax.sql.DataSource;

import org.springframework.batch.core.repository.ExecutionContextSerializer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.dao.ExecutionContextDao;
import org.springframework.batch.core.repository.dao.JobExecutionDao;
import org.springframework.batch.core.repository.dao.JobInstanceDao;
import org.springframework.batch.core.repository.dao.StepExecutionDao;
import org.springframework.batch.core.repository.dao.XStreamExecutionContextStringSerializer;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory;
import org.springframework.batch.support.DatabaseType;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;

public class MarvelJobRepositoryFactoryBean extends JobRepositoryFactoryBean implements InitializingBean {
	
	private DataSource dataSource;

	private ExecutionContextSerializer serializer;
	
	private JobRepository jobRepository;
	
	/**
	 * @return fully configured {@link JobInstanceDao} implementation.
	 */
	@Override
	public JobInstanceDao createJobInstanceDao() throws Exception{
		return super.createJobInstanceDao();
	}

	/**
	 * @return fully configured {@link JobExecutionDao} implementation.
	 */
	@Override
	public JobExecutionDao createJobExecutionDao() throws Exception{
		return super.createJobExecutionDao();
	}

	/**
	 * @return fully configured {@link StepExecutionDao} implementation.
	 */
	@Override
	public StepExecutionDao createStepExecutionDao() throws Exception{
		return super.createStepExecutionDao();
	}

	/**
	 * @return fully configured {@link ExecutionContextDao} implementation.
	 */
	@Override
	public ExecutionContextDao createExecutionContextDao() throws Exception{
		return super.createExecutionContextDao();
	}
	
	@Override
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		super.setDataSource(this.dataSource);
	}

	@Override
	public void setSerializer(ExecutionContextSerializer serializer) {
		this.serializer = serializer;
		super.setSerializer(this.serializer);
	}
	
	public void setJobRepository(JobRepository jobRepository) {
		this.jobRepository = jobRepository;
	}

	@Override
	public void afterPropertiesSet() throws Exception {

		super.setJdbcOperations(new JdbcTemplate(this.dataSource));
		
		super.setIncrementerFactory(new DefaultDataFieldMaxValueIncrementerFactory(this.dataSource));

		super.setDatabaseType(DatabaseType.fromMetaData(this.dataSource).name());

		if(null == serializer){
			XStreamExecutionContextStringSerializer defaultSerializer = new XStreamExecutionContextStringSerializer();
			defaultSerializer.afterPropertiesSet();
			this.setSerializer(defaultSerializer);
		}
		
		if(null == jobRepository){
			this.setJobRepository(new MarvelJobRepository(createJobInstanceDao(),
					createJobExecutionDao(), 
					createStepExecutionDao(), 
					createExecutionContextDao()));
		}
	}
//
//	@Override
//	public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
//		// TODO Auto-generated method stub
//		return jobRepository.isJobInstanceExists(jobName, jobParameters);
//	}
//
//	@Override
//	public JobInstance createJobInstance(String jobName, JobParameters jobParameters) {
//		// TODO Auto-generated method stub
//		return jobRepository.createJobInstance(jobName, jobParameters);
//	}
//
//	@Override
//	public JobExecution createJobExecution(JobInstance jobInstance, JobParameters jobParameters,
//			String jobConfigurationLocation) {
//		// TODO Auto-generated method stub
//		return jobRepository.createJobExecution(jobInstance, jobParameters, jobConfigurationLocation);
//	}
//
//	@Override
//	public JobExecution createJobExecution(String jobName, JobParameters jobParameters)
//			throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
//		// TODO Auto-generated method stub
//		return jobRepository.createJobExecution(jobName, jobParameters);
//	}
//
//	@Override
//	public void update(JobExecution jobExecution) {
//		// TODO Auto-generated method stub
//		jobRepository.update(jobExecution);
//	}
//
//	@Override
//	public void add(StepExecution stepExecution) {
//		// TODO Auto-generated method stub
//		jobRepository.add(stepExecution);
//	}
//
//	@Override
//	public void addAll(Collection<StepExecution> stepExecutions) {
//		// TODO Auto-generated method stub
//		jobRepository.addAll(stepExecutions);
//	}
//
//	@Override
//	public void update(StepExecution stepExecution) {
//		// TODO Auto-generated method stub
//		jobRepository.update(stepExecution);
//	}
//
//	@Override
//	public void updateExecutionContext(StepExecution stepExecution) {
//		// TODO Auto-generated method stub
//		jobRepository.updateExecutionContext(stepExecution);
//	}
//
//	@Override
//	public void updateExecutionContext(JobExecution jobExecution) {
//		// TODO Auto-generated method stub
//		jobRepository.updateExecutionContext(jobExecution);
//	}
//
//	@Override
//	public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {
//		// TODO Auto-generated method stub
//		return jobRepository.getLastStepExecution(jobInstance, stepName);
//	}
//
//	@Override
//	public int getStepExecutionCount(JobInstance jobInstance, String stepName) {
//		// TODO Auto-generated method stub
//		return jobRepository.getStepExecutionCount(jobInstance, stepName);
//	}
//
//	@Override
//	public JobExecution getLastJobExecution(String jobName, JobParameters jobParameters) {
//		// TODO Auto-generated method stub
//		return jobRepository.getLastJobExecution(jobName, jobParameters);
//	}

	@Override
	public JobRepository getObject(){
		return jobRepository;
		
	}
}
